// pages/permission/selectPlot/selectPlot.js
import http from '../../../utils/fetch.js'
import api from '../../../utils/api.js'
import QQMapWX from '../.././../utils/qqmap-wx-jssdk.min'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    CID:'',
    lat: 0,
    lon: 0,
    plotList:[],
    handleCity:'',
    handleCityId:'',
  },

  getLocal (lat, lon,cid,city) {
    
    var that = this;
    let qqmapsdk = new QQMapWX({
      key: 'HRKBZ-MOT36-TR2SF-EFC6P-QOWKK-4IFHK'
    })
    if (that.data.handleCityId) {
      var mydata = {
        CID: cid,
        limit:10000
      }
      http.post(api.getPlot, mydata).then((res) => {
        if(res.code == 0) {
          that.setData({
            plotList: res.data
          })
        } else {
          console.log('请求失败')
        }
      })

    }
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lon
      },
     
      success: function (res) {
        
        that.setData({
          CID: res.result.ad_info.adcode.slice(0, 2),
          city: res.result.address_component.city
        })
        // var mydata={}
     
          var mydata = {
            CID: res.result.ad_info.adcode.slice(0,4),
            limit:10000
          }
        
       
        
        http.post(api.getPlot, mydata||mydataHandle).then((res) => {
          if(res.code == 0) {
            that.setData({
              plotList: res.data
            })
          } else {
            console.log('请求失败')
          }
        })
      },
      fail: function (res) {
        console.log(res, 'fail')
      }
    })
  },
  refresh: function (){
    this.getLocal(this.data.lat,this.data.lon);
    
  },
  toSelectCity: function() {
    wx.navigateTo({
      url: '../selectCity/selectCity',
    })
  },
  toSelectFloor:function(e) {
    var that = this;

    console.log(that.data.plotList[e.currentTarget.dataset.index]._id)
    wx.navigateTo({
      // url: '../selectFloor/selectFloor?plot='+this.data.plotList[e.currentTarget.dataset.index]+'city='+this.data.city,
      url: '../selectFloor/selectFloor?obj='+JSON.stringify({plot:that.data.plotList[e.currentTarget.dataset.index].name,CID:that.data.plotList[e.currentTarget.dataset.index].CID,city:this.data.city})
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
  onShow:function(options){
    
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    let jsonObject = currPage.data.mydata;
    if (jsonObject) {
      this.setData({
        handleCityId:jsonObject.cid||'',
        city:jsonObject.city||''
      })
    }
    
    console.log(jsonObject,"jjjjjjjjjjjj")
    
    this.getLocal('', '',jsonObject.cid,jsonObject.city)
  },
  onLoad: function (options) {
    
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        that.setData({
          lat: res.latitude,
          lon: res.longitude
        })
        const lat = res.latitude
        const lon = res.longitude
        that.getLocal(lat, lon,'','')
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
 
  /**
   * 生命周期函数--监听页面显示
   */
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})